#include "global.h"


// private variables declaration 

union
{
	struct
	{
		unsigned 	COMM:1;
		unsigned	HPS:1;
		unsigned	Supply:1;
		unsigned 	Green:1;
		unsigned	Yellow:1;
		unsigned	Red:1;
		unsigned	Spare:2;
	
	}pin;
	unsigned char latch;
}OUT;

union
{
	struct
	{
		unsigned 	Start:1;
		unsigned	Stop:1;
		unsigned	EEV_contact:1;
		unsigned 	Supply_present:1;
		unsigned	Comm_in_danger:1;
		unsigned 	RV_present:1;
		unsigned	:2;
	}pin;
	unsigned char data;
}IN;




void ConnectSupply(void)
{
OUT.pin.Supply=1;
OUTPORT=OUT.latch;
};

void DisconnectSupply(void)
{
OUT.pin.Supply=0;
OUTPORT=OUT.latch;
};

void ConnectHPS(void)
{
OUT.pin.HPS=1;
OUTPORT=OUT.latch;

};

void DisconnectHPS(void)
{
OUT.pin.HPS=0;
OUTPORT=OUT.latch;
};

unsigned char SupplyPresent(void)
{		
//	IN.data=PORTD;
	return !IN.pin.Supply_present;
}; 

unsigned char StartButtonPressed(void)
{
	return !IN.pin.Start;

};
unsigned char StopButtonPressed(void)
{

	return !IN.pin.Stop;

};

unsigned char EEVState(void)
{
	return !IN.pin.EEV_contact;
};

unsigned char RVState(void)
{
	return !IN.pin.RV_present;
};


unsigned char ComInDanger(void)
{
//	IN.data=PORTD;
	return !(IN.pin.Comm_in_danger);
};

void ConnectCOM(void)
{
	OUT.pin.COMM=1;
	OUTPORT=OUT.latch;
};
void DisconnectCOM(void)
{
	OUT.pin.COMM=0;
	OUTPORT=OUT.latch;
};





bit flag_red_lamp_on;
bit flag_green_lamp_on;
bit flag_yellow_lamp_on;
bit flag_red_lamp_blink;
bit flag_green_lamp_blink;
bit flag_yellow_lamp_blink;
bit flag_blinkled;



void ServiceLamps(void)
{	static bank3 unsigned int blink_counter;
	
	if ( blink_counter++ > LAMP_BLINK_PERIOD )
	{	blink_counter=0;
		if (flag_red_lamp_blink && flag_red_lamp_on)
		{
			OUT.pin.Red ^=1;
		}
		else 	
		{
			OUT.pin.Red = flag_red_lamp_on;
		};

		if (flag_green_lamp_blink && flag_green_lamp_on)
		{
			OUT.pin.Green^=1;				
		}
		else 	
		{
			OUT.pin.Green=flag_green_lamp_on;
		};
		flag_blinkled^=1;
		RA4=flag_blinkled;

		if (flag_yellow_lamp_blink && flag_yellow_lamp_on)
		{
			OUT.pin.Yellow^=1;				
		}
		else 	
		{
			OUT.pin.Yellow=flag_yellow_lamp_on;
		};
		OUTPORT=OUT.latch;	
	};
};


void ServiceInputs(void)
{	static unsigned char mask,vcount0,vcount1,prevstate;
	unsigned char temp;
	temp = INPORT;
	mask=temp^prevstate;
	vcount0&=mask;
	vcount1&=mask;
	prevstate^ =( mask&vcount0&vcount1);
	vcount0^=mask;
	vcount1^=(vcount0^mask);
	IN.data=prevstate;

// possible to use IN.data directly instead of prevstate ??????????????
}















